package coding.musicPlay;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;

/**
 * @创建人 xfliu
 * @创建时间 2021/9/17
 * @描述音乐播放器
 */


class ThreadPlayer implements Runnable{
    private String path;
    private MyPlayer myPlayer;
    public ThreadPlayer(String path,MyPlayer myPlayer){
        this.path=path;
        this.myPlayer=myPlayer;
    }
    @Override
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName()+" runing~~~");
            Thread.currentThread().interrupt();
            System.out.println(Thread.currentThread().isInterrupted());
            this.myPlayer.playMusic(path);

        } catch (FileNotFoundException | JavaLayerException e) {
            e.printStackTrace();
        }

    }
}

//多线程播放
public class MyPlayer {
    //播放音乐
    Player player;
    public MyPlayer(Player player){
        this.player=player;
    }

    public synchronized void playMusic(String musicPath) throws FileNotFoundException, JavaLayerException {
        File file=new File(musicPath);
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream stream = new BufferedInputStream(fis);
        player = new Player(stream);
        player.play();

        System.out.println("finished~");
//        player.close();
    }
    //暂停音乐
    public void pauseMusic(){}
    //停止音乐
    public void stopMusic(){}

    public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
        String musicPath="D:\\酷狗\\薛之谦-演员.mp3";
        Player player = null;
        MyPlayer myPlayer=new MyPlayer(player);
//        myPlayer.playMusic(musicPath);

        for (int i=0;i<3;i++){


            ThreadPlayer threadPlayer=new ThreadPlayer(musicPath,myPlayer);
            Thread thread = new Thread(threadPlayer);

            thread.start();

            try {
                Thread.sleep(3000);
//                thread.stop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}

